package com.parenting.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.parenting.entities.CouponDO;
import com.parenting.enums.ResultEnum;
import com.parenting.exception.BaseException;
import com.parenting.mapper.CouponMapper;
import com.parenting.model.IMPage;
import com.parenting.request.*;
import com.parenting.response.ActivityCouponListResponse;
import com.parenting.response.CouponDetails;
import com.parenting.response.CouponDropDown;
import com.parenting.response.CouponListResponse;
import com.parenting.service.CouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 优惠券 服务实现类
 * </p>
 *
 * @author 烟台业达-王世玺
 * @since 2021-08-27
 */
@Service
@Slf4j
public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponDO> implements CouponService {

    @Autowired
    private RedissonClient redissonClient;

    @Override
    public void addCoupon(CouponAddRequest couponAddRequest, String shopId) {
        CouponDO couponDO = BeanUtil.copyProperties(couponAddRequest, CouponDO.class);
        couponDO.setShopId(shopId);
        couponDO.setFulls(couponDO.getFulls()*1000);
        couponDO.setReduce(couponDO.getReduce()*1000);
        Date date=new Date();
        couponDO.setUpdatedTime(date);
        couponDO.setCreatedTime(date);
        baseMapper.insert(couponDO);
    }

    @Override
    public synchronized  void  updateCouponById(CouponUpdateRequest request) {
        String lockKey = "lock:coupon:"+request.getCouponId();
        RLock rLock = redissonClient.getLock(lockKey);
        rLock.lock(5, TimeUnit.SECONDS);
       try {
           CouponDO couponDO = baseMapper.selectById(request.getCouponId());
           couponDO.setTotal(request.getTotal());
           baseMapper.updateById(couponDO);
       }finally{
           rLock.unlock();
           log.info("解锁成功");
        }
    }

    @Override
    public  IMPage <CouponListResponse> getPageListCoupon(CouponListSearchCriteriaRequest request, String shopId) {
        IPage<CouponListResponse> pageListCoupon = baseMapper.getPageListCoupon(request.getPage(), shopId);
        IMPage <CouponListResponse> page=new IMPage<>();
        page.setTotal(pageListCoupon.getTotal());
        page.setSize(pageListCoupon.getSize());
        page.setCurrent(pageListCoupon.getCurrent());
        page.setRecords(pageListCoupon.getRecords());
        return page;
    }


    @Override
    public IPage<ActivityCouponListResponse> getActivityCouponConsumptionList(String shopId,ActivityCouponConsumptionListRequest request) {

        return baseMapper.getActivityCouponConsumptionList(request.getPage(),shopId,request);
    }


    @Override
    public CouponDetails getCouponByCouponId(String couponId) {
        CouponDO couponDO = baseMapper.selectById(couponId);
        CouponDetails couponDetails = BeanUtil.copyProperties(couponDO, CouponDetails.class);
        return  couponDetails;
    }

    @Override
    public List<CouponDropDown> getCouponDropDown(String shopId, String type) {
        CouponDropDownRequest couponDropDownRequest=new CouponDropDownRequest();
        couponDropDownRequest.setShopId(shopId);
        couponDropDownRequest.setType(type);

        return baseMapper.getCouponDropDown(couponDropDownRequest);
    }

    @Override
    public synchronized void   updateNumberOfCouponCouponById(int number, String id) {
        String lockKey = "lock:coupon:number:"+id;
        RLock rLock = redissonClient.getLock(lockKey);
        rLock.lock(5, TimeUnit.SECONDS);
        try {
            CouponDO couponDO = baseMapper.selectById(id);
            if (couponDO.getTotal()<=number){
                throw new BaseException(ResultEnum.COMMODITY_NO_STOCK);
            }
            couponDO.setTotal((couponDO.getTotal()-number));
            baseMapper.updateById(couponDO);
        }finally{
            rLock.unlock();
            log.info("解锁成功");
        }
    }

    @Override
    public void updateNumberOfCouponCouponById(int number, String id, String type) {
        String lockKey = "lock:coupon:number:"+id;
        RLock rLock = redissonClient.getLock(lockKey);
        rLock.lock(5, TimeUnit.SECONDS);
        try {
            CouponDO couponDO = baseMapper.selectById(id);
            if ("add".equals(type)){
                couponDO.setTotal((couponDO.getTotal()+number));
            }else{
                couponDO.setTotal((couponDO.getTotal()-number));
            }
            baseMapper.updateById(couponDO);
        }finally{
            rLock.unlock();
            log.info("解锁成功");
        }
    }


}
